<?php

/**
* eTpl 是一个编译型的小型模板程序,配合emvc使用。

*
* @author Junlei Zhang <zhangjunlei26@gmail.com>
* @version 1.0.1 04.11.2007
*
*/
class ETpl extends EObject {
	
	/**
	* 是否存储被编译的php代码(调试目的)
	*
	* @access public
	*/
	/**
	* Temporary file for output cache storage
	*
	* @access private
	*/
	var $cache_file;

	/**
	* The template filename
	*
	* @access private
	*/
	var $tpl_file;

	/**
	* The compiled template filename
	*
	* @access private
	*/
	var $cpl_file;
	/**
	 * 
	 */
	var $type;
	/**
	* Template content array
	*
	* @access private
	*/
	var $data;
	
	var $_main_setting;

	/**
	* Parser Class
	*
	* @access private
	*/
	var $parser;

	/**
	* eTpl Constructor
	* <code>
	* $options=array(
			'tpl_file'=>'',
			'cpl_file'=>'',
			'cache_file'=>'',
		);
		$tpl=new eTpl($options);
	* </code>
	*
	* @access public
	* @param string $template_filename Template Filename
	*/
	function __construct(&$options) {
		if($options){
			parent::setFrom($options);
		}
	}
	function ETpl($options = array ()) {
		$this->__construct($options);
	}
	function callback(&$buffer) {
		if (empty($this->cache_file)){
				trigger_error('Error:The name of Cache file not set.',E_USER_ERROR);
		}
		return $this->save($buffer, $this->cache_file);
	}
	function &save(&$buffer, $file) {
		if ($hd = fopen($file, 'w')) {
			fputs($hd, $buffer);
			fclose($hd);
		}
		return $buffer;
	}
	function display($cache=true){
		if(!file_exists($this->tpl_file)){
			trigger_error('Template file ['.$this->tpl_file.'] not found.',E_USER_ERROR);
		}
		$cache_flag=$cache && file_exists($this->cache_file) && filemtime($this->cache_file)>filemtime($this->tpl_file);
		if($cache_flag){
			return readfile($this->cache_file);
		}
		$compile_flag=!file_exists($this->cpl_file) || filemtime($this->cpl_file)<filemtime($this->tpl_file);
		if ($compile_flag){
			$this->compile($this->tpl_file,$this->cpl_file);
		}
		if ($cache){
			ob_start(array (&$this,'callback'));
		}else{
			ob_start();//'gz_handler'
		}
		$registry=&registry();
	 
		if(!empty($registry->registry['default'])){
			extract($registry->registry['default'],EXTR_REFS);
			extract($registry->registry);
		}
		if(!include($this->cpl_file)){
			trigger_error('Error: Compiled Template Does Not Exists.', E_USER_ERROR);
		}
		ob_end_flush();
	}
	function compile($tpl='',$cpl='') {
		$parser=&$this->_getParser();
		$parser->compile($tpl,$cpl);
	}
	function &_getParser(){
		static $instance;
		if(!isset($instance)){
			if(include_once(dirname(__FILE__).DS .'etplparser.php')){
				$instance=new eTplParser();
			}else{
				trigger_error('Error: Can not find template parser.',E_USER_ERROR);
			}
		}
		return $instance;
	}
	function get_result(&$data, $cache = false) {
		if($data)$this->set_from($data);
		if (file_exists($this->cache_file)){
			readfile($this->cache_file);
			$result = ob_get_contents();
			ob_end_clean();
			return $result;
		}
		if (!file_exists($this->cpl_file) || filemtime($this->cpl_file)<filemtime($this->tpl_file))
			$this->compile();
		if ($this->static_cache_use)
			ob_start(array (& $this,'callback'));
		else{
			ob_start();
		}
		if(file_exists($this->cpl_file)){
			include ($this->cpl_file);
		}else{
			trigger_error('Compiled template not found.',E_USER_ERROR);
		}
		unset ($this->data);
		$result = ob_get_contents();
		ob_end_clean();
		return $result;
	}
	function & get_data() {
		return $this->data;
	}
	function __destruct() {
		unset($this->data);
	}
}
?>
